// pages/my/collection/list.js
const App = getApp()
const api = require('../../../utils/api.js');
const util = require('../../../utils/util.js');


Page({
  data: {
    defaultimg: 'http://oss-img.meitianhui.com/test/communityDocs/201801/51a75f1fc3f445cba238b145faff1d41.jpg',
    collection: {
      items: [
        /**
         * 
            {
              id: 1,
              name: '领有惠便利店',
              address: '北京市东城区北新桥',
              isDefault: 1,
              isCollect: 1,
              icon: '../../../image/u2956.png'
            },
            {
              id: 2,
              name: '乐购乐',
              address: '北京市东城区北新桥',
              isDefault: 0,
              isCollect: 1,
              icon: '../../../image/u2956.png'
            },
            {
              id: 3,
              name: '土地土',
              address: '北京市东城区哈哈哈',
              isDefault: 0,
              isCollect: 1,
              icon: '../../../image/u2956.png'
            }
         */

      ],
      params: {
      },
      paginate: {}
    },

    prompt: {
      hidden: 1,
      icon: '/image/empty.png',
      title: '您还没有收藏店铺！',
      text: '赶快去收藏哦',
    },
  },

  onLoad() {
    this.onPullDownRefresh()
  },


  initData() {
    this.setData({
      collection: {
        items: [],
        params: {
          page: 0,
          limit: 10,
        },
        paginate: {}
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  ///收藏门店列表（我的收藏）
  favoriteStoreList: function (e) {

    let paramsData = {
      consumer_id: App.globalData.memberInfo.member_id,
    }

   //收藏门店列表
    const data = {
      service: 'stores.consumer.favoriteStoreList',
      params: JSON.stringify(paramsData),
      url_type: 'member',

      app_token: App.globalData.app_token,        
      security_code: App.globalData.security_code  
    }


    var that = this

    const collection = this.data.collection
    const prompt = this.data.prompt
    //const params = collection.params


    api.reqData({
      data,
      success: (res) => {
        console.log("返回 res.data>>>" + res.data);
        var resData = res.data;
        var docList = resData.data.doc_url;
        var itemList = resData.data.list;

        var doc_url_key = Object.keys(docList)
        for (var i = 0; i < itemList.length; i++) {


          var item_pic_info_str = itemList[i].neighbor_pic_path;

          if (item_pic_info_str!=''){
            for (var e = 0; e < doc_url_key.length; e++) {
              if (item_pic_info_str === doc_url_key[e]) {
                itemList[i].pic_info = docList[doc_url_key[e]].replace("https", "http")
              }
            }
          }else{
            itemList[i].pic_info = ''
          }
        }

        collection.items = itemList
        collection.paginate.total = resData.data.list.length
        prompt.hidden = collection.items.length

        this.setData({
          collection: collection,
          //'prompt.hidden': cash.items.length,
          prompt: prompt,
        })

      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })

  },


  /**店铺详细 */
  toStoreDetail(e) {


    console.log("e>>>>eeee>>>>eee>>>>>>>>>>>>>>>>>>>>>>>")
console.log(e)
    console.log(e.currentTarget.dataset.storeid)

    var storeId = e.currentTarget.dataset.storeid;
    wx.navigateTo({ url: '/pages/detail/detail?stores_id=' + storeId })
  },


  /**设置默认门店 */
  toSetDef(e) {
    var defStoreId = e.target.dataset.storeid;

    const paramsData = {
      consumer_id: App.globalData.memberInfo.member_id,
      is_llm_stores: 'Y',
      stores_id: defStoreId,
    }
    //设置默认门店列表接口
    const data = {
      service: 'stores.consumer.favoriteStore',
      params: JSON.stringify(paramsData),
      url_type: 'member',
      app_token: App.globalData.app_token,         //add
      security_code: App.globalData.security_code  //add

    }


    api.reqData({
      data,
      success: (res) => {
        console.log("返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        this.onPullDownRefresh()
      },
      fail: (res) => {
        console.log(res.data);
      }
    })


    /**
     * 
        for (var i = 0; i < this.data.collection.items.length; i++) {
          if (this.data.collection.items[i].id == defStoreId) {
            this.data.collection.items[i].isDefault = 1
          } else {
            this.data.collection.items[i].isDefault = 0
          }
        }
        this.setData({
          collection: this.data.collection,
        })
     */



  },






  /**取消收藏 */
  toCancelCollection(e) {
    var storeId = e.target.dataset.storeid;
    console.log("storeId>>>>>>>>>>>" + storeId);

    const paramsData = {
      consumer_id: App.globalData.memberInfo.member_id,
      stores_id: storeId,
    }
   //取消收藏门店接口
    const data = {
      service: 'stores.consumer.favoriteStoreCancel',
      params: JSON.stringify(paramsData),
      url_type: 'member',

      app_token: App.globalData.app_token,         //add
      security_code: App.globalData.security_code  //add

    }

    api.reqData({
      data,
      success: (res) => {
        console.log("返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        this.onPullDownRefresh()
      },
      fail: (res) => {
        console.log(res.data);
      }
    })

    //temple start
    /**
    var ii;
    for (var i = 0; i < this.data.collection.items.length; i++) {
      if (this.data.collection.items[i].id == storeId){
        console.log("items" + this.data.collection.items[i])
        this.data.collection.items[i].isCollect = 0
      }
    }

    this.setData({
      collection: this.data.collection,
    })
    */
    //end

  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.favoriteStoreList()
    //模拟加载
    wx.showLoading({
      title: '加载中',
    })

    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  },


  /**
   * 页面上拉触底事件的处理函数
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.collection.paginate.hasNext) return
    this.favoriteStoreList()
  },

   */

})